Vue.filter('improve', function (value) {
    return "￥" + value.toFixed(2)
})

let app = new Vue({
    el:"#app",
    data: {
        msg: "hello",
        books:[
            {
                id:1,
                name:"《算法导论》",
                data:"2006-9",
                price: 85,
                count: 1
                
            },
            {
                id: 2,
                name: '《UNIX编程艺术》',
                data: '2006-2',
                price: 59,
                count: 1 

            },
            {
                id: 3,
                name: '《编程珠玑》',
                data: '2008-10',
                price: 39,
                count: 1

            },
            {
                id: 4,
                name: '《代码大全》',
                data: '2006-3',
                price: 128,
                count: 1

            },
        ]
    },
    methods:{
        increase(index){
            this.books[index].count++
        },
        decrease(index){
            this.books[index].count--
        },
        remove(index){
            //this.books.pop()
            this.books.splice(index,1)

        },
        showPrice(price) {
            return "￥" + price.toFixed(2)
        }
    },
    computed: {
        totalPrice() {
            // let totalPrice = 0
            // for (const item of this.books) { //通过for循环实现的
            //     totalPrice += item.price * item.count
            // }
            // return totalPrice
            return this.books.reduce(function(preValue,n){
               return preValue + n.price * n.count
            },0)
        }
    },
    filters: {
        improvePrice: function (value) {
            return "￥" + value.toFixed(2)
        }
    }
})
